{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a71e4545-62cd-4532-83a7-2fe72b097881",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "499e27ac-d19c-4a9e-a5b2-cdbc862c9343",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63c39512-669c-4bc0-ae34-ba86a6174eee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请注意，如果你使用的是课程提供的 API 密钥，上面单元格代码初始化 OpenAI 类实例时还需要把 base_url 参数赋值为相应地址：\n",
    "# client = OpenAI(api_key=【你的密钥】,\n",
    "#                 base_url=\"https://api.aigc369.com/v1\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3580d2f3-0067-46fd-aad9-c4d77fc291b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = f\"\"\"\n",
    "生成一个由三个虚构的订单信息所组成的列表，以JSON格式进行返回。\n",
    "JSON列表里的每个元素包含以下信息：\n",
    "order_id、customer_name、order_item、phone。\n",
    "所有信息都是字符串。\n",
    "除了JSON之外，不要输出任何额外的文本。\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3adaca24-631e-4c0b-a219-e5c53ba02c1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = client.chat.completions.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=[\n",
    "    {\n",
    "      \"role\": \"user\",\n",
    "      \"content\": prompt\n",
    "    }\n",
    "  ]\n",
    ")\n",
    "content = response.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1df486c3-8931-4c90-a24a-c51bec38f5fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'order_id': '123',\n",
       "  'customer_name': 'John Smith',\n",
       "  'order_item': 'Shoes',\n",
       "  'phone': '1234567890'},\n",
       " {'order_id': '456',\n",
       "  'customer_name': 'Jane Doe',\n",
       "  'order_item': 'T-shirt',\n",
       "  'phone': '9876543210'},\n",
       " {'order_id': '789',\n",
       "  'customer_name': 'Tom Thompson',\n",
       "  'order_item': 'Jeans',\n",
       "  'phone': '4567890123'}]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json.loads(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2ebdb8f5-e5c2-4119-9243-c70aec7f41ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1234567890'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json.loads(content)[0][\"phone\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38a41750-eec1-4051-9c54-7ffbaa8c6bcc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
